﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using EzDesk.Utility;

namespace EzDesk.Core.Model
{
    public static class EzDeskAppApiHelper
    {
        public static bool ContainUser(this IEnumerable<SimpleUser> userList, Guid userId)
        {
            foreach (var user in userList)
            {
                if (user.Id == userId)
                {
                    return true;
                }
            }
            return false;
        }

        public static void DelaySendUpdateFeeds<T>(
            T oldObj, T newObj,
            Guid actorId, Func<SendUpdateFeedsMethod<T>, bool> cachedMethodCondition, SendUpdateFeedsDelegate<T> sendUpdateFeedsDelegate)
             where T : class, ICloneable<T>
        {
            var updateTaskMethods = HttpContext.Current.Application["SendUpdatesMethods"] as List<SendUpdateFeedsMethod>; ;
            if (updateTaskMethods == null) updateTaskMethods = new List<SendUpdateFeedsMethod>();

            var cachedMethod =
                updateTaskMethods.FirstOrDefault(
                    p => p is SendUpdateFeedsMethod<T> &&
                        cachedMethodCondition(p as SendUpdateFeedsMethod<T>))
                as SendUpdateFeedsMethod<T>;
            if (cachedMethod == null)
            {
                updateTaskMethods.Add(new SendUpdateFeedsMethod<T>
                {
                    ActorId = actorId,
                    Method = sendUpdateFeedsDelegate,
                    OldObject = oldObj == null ? null : oldObj.Clone(),
                    NewObject = newObj == null ? null : newObj.Clone(),
                    UpdateOn = DateTime.Now,
                });
                HttpContext.Current.Application["SendUpdatesMethods"] = updateTaskMethods;
            }
            else
            {
                cachedMethod.NewObject = newObj.Clone();
                cachedMethod.UpdateOn = DateTime.Now;
            }
        }


        public static class MemberStringHelper
        {

            public static string GetMemberString(List<SimpleUser> simpleUsers)
            {
                return null;
            }

            public static string GetMemberString(List<UserProfile> userProfiles)
            {
                return null;
            }

            public static string JoinUserId(string originalString, Guid userId)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                string resultString = null;

                int splitorIndex = originalString.IndexOf('|');

                if (splitorIndex == 0)
                {
                    // '|' 出现在第一个，源字符串只有group，没有UserId
                    resultString = string.Format("{0}{1}", userId, originalString);
                }
                else if (splitorIndex > 0)
                {
                    // '|' 出现在中间，源字符串有UserId
                    resultString = string.Format("{0},{1}", userId, originalString);
                }
                else if (splitorIndex < 0)
                {
                    // '|' 不存在，源字符串中没有group数据
                    if (originalString.Length == 0)
                    {
                        // 源字符串为空
                        resultString = userId.ToString();
                    }
                    else
                    {
                        resultString = string.Format("{0},{1}", userId, originalString);
                    }
                }
                return resultString;
            }

            public static string RemoveUserId(string originalString, Guid userId)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                var userIdStr = userId.ToString();
                if (originalString.Contains(userId.ToString()))
                {
                    // 从原来的字符串中把UserId删除
                    var index = originalString.IndexOf(userId.ToString());
                    originalString = originalString.Remove(index, userIdStr.Length);
                    if (originalString.StartsWith(","))
                    {
                        originalString = originalString.Remove(0, 1);
                    }
                    originalString = originalString.Replace(",,", ",").Replace(",|", "|");
                }
                return originalString;
            }

            public static string JoinUserIds(string originalString, params Guid[] userIds)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                string resultString = null;

                var userIdList = new StringBuilder();
                var index = 0;
                foreach (var userId in userIds)
                {
                    index = index + 1;
                    if (index < userIds.Length)
                    {
                        userIdList.Append(userId.ToString());
                        userIdList.Append(",");
                    }
                    else
                    {
                        userIdList.Append(userId.ToString());
                    }

                }

                var splitorIndex = originalString.IndexOf('|');

                if (splitorIndex == 0)
                {
                    // '|' 出现在第一个，源字符串只有group，没有UserId
                    resultString = string.Format("{0}{1}", userIdList + originalString);
                }
                else if (splitorIndex > 0)
                {
                    // '|' 出现在中间，源字符串有UserId
                    resultString = string.Format("{0},{1}", userIdList, originalString);
                }
                else if (splitorIndex < 0)
                {
                    // '|' 不存在，源字符串中没有group数据
                    resultString = originalString.Length == 0 ?
                        userIdList.ToString() : string.Format("{0},{1}",
                        userIdList, originalString);
                }
                return resultString;
            }

            public static string JoinGroupId(string originalString, Guid groupId)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                string resultString = null;

                int splitorIndex = originalString.IndexOf('|');

                if (splitorIndex >= 0)
                {
                    // '|' 出现在中间，源字符串有UserId
                    // xxx,xxx|zzz,zzz
                    resultString = string.Format("{0},{1}", groupId.ToString(), originalString);
                }
                else if (splitorIndex < 0)
                {
                    // '|' 不存在，源字符串中没有group数据
                    resultString = string.Format("{0}|{1}", originalString, groupId.ToString());
                }
                return resultString;

            }

            public static string GetIdStrdFromIdList(List<Guid> list)
            {
                var feedIdArray = list.Distinct().Select(p => p.ToString());
                return feedIdArray.Join(",");
            }
        }

    }
}
